package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.DuihuanInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.DuihuanPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
* Created by Hu on 2016/12/08
*/

public class DuihuanModelImpl implements DuihuanModel, VolleyJsonObjectCallBack {
    DuihuanPresenterImpl duihuanPresenter;
    @Override
    public void sendUrl(String url, DuihuanPresenterImpl duihuanPresenter) {
        this.duihuanPresenter=duihuanPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void sendDuihuanUrl(String url,DuihuanPresenterImpl duihuanPresenter) {
        this.duihuanPresenter=duihuanPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    duihuanPresenter.sendRes(res);
                } catch (JSONException e) {

                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            duihuanPresenter.sendError(error);
            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<DuihuanInfo> duihuanInfos = JsonUtil.getListFromString(response.getString("res"),DuihuanInfo.class);
            duihuanPresenter.sendList(duihuanInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
            duihuanPresenter.sendError(error);
    }
}